package commands;

import java.io.PrintWriter;

import memory.Memory;

import exceptions.ParseException;
import expression.Expression;
import expression.Sum;

public class PrintCommand extends Command {

	Command next;
	PrintWriter destination;
	Expression expression;

	public PrintCommand(int lineNumber, String command,
			PrintWriter destination, Memory memory) throws ParseException {
		super(lineNumber);
		this.destination = destination;
		command = fixString(command.substring(command.indexOf("print") + 5));
		expression = new Sum(command, memory);
	}

	@Override
	public void run() {
		destination.println(expression.calculate());
		destination.flush();
	}

	@Override
	public Command getNext() {
		return next;
	}

	@Override
	public void setNext(Command command) {
		next = command;
	}
}
